package com.zzx.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.emus.IsBuddy;
import com.zzx.boot.entity.Buddy;
import com.zzx.boot.entity.dto.AddBuddy;
import com.zzx.boot.entity.vo.BuddyInfoVo;
import com.zzx.boot.entity.vo.BuddyVo;
import com.zzx.boot.entity.vo.UserInfoVo;
import com.zzx.boot.mapper.BuddyMapper;
import com.zzx.boot.mapper.UserMapper;
import com.zzx.boot.service.BuddyService;
import com.zzx.boot.utils.SecurityContext;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

@Service
public class BuddyServiceImpl extends ServiceImpl<BuddyMapper,Buddy> implements BuddyService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private SecurityContext securityContext;
    @Autowired
    private BuddyMapper buddyMapper;

    @Override
    public Boolean isBuddyByUserId(Long userId, Long userId1,Integer status) {
        LambdaQueryWrapper<Buddy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(status!=-1,Buddy::getStatus,status);

        wrapper.and(andWrapper->{
            andWrapper.or(wrapper3->{
                wrapper3.eq(Buddy::getInitiatorId,userId);
                wrapper3.eq(Buddy::getReceiverId,userId1);
            });
            andWrapper.or(wrapper2->{
                wrapper2.eq(Buddy::getInitiatorId,userId1);
                wrapper2.eq(Buddy::getReceiverId,userId);
            });
        });
        Buddy buddy = baseMapper.selectOne(wrapper);
        return Objects.nonNull(buddy);
    }

    @Override
    public String addBuddy(AddBuddy buddy) {
        //先查一下 有没有添加过
        // 构建对象
        Long userId = securityContext.getUserId();
        Long second = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();;
        if (this.isBuddyByUserId(userId, buddy.getUserId(),-1)) {
            return  "不可重复添加";
        }

        Buddy buddy1 = new Buddy(null, userId, buddy.getUserId(), userId, second, buddy.getSearchType(), buddy.getTag(),buddy.getRemark(), 0);
        int insert = baseMapper.insert(buddy1);
        if(insert!=1){
           throw new RuntimeException("添加失败");
        }
        return "添加成功";
    }

    @Override
    public List<BuddyVo> buddyListByUserId() {
        LambdaQueryWrapper<Buddy> wrapper = new LambdaQueryWrapper<>();
        Long userId = securityContext.getUserId();
        wrapper.eq(Buddy::getInitiatorId,userId);
        wrapper.or();
        wrapper.eq(Buddy::getReceiverId,userId);
        List<Buddy> list = baseMapper.selectList(wrapper);
        // 添加 用户信息
        List<BuddyVo> buddyVos = list.stream().map(buddy -> {
            BuddyVo vo = new BuddyVo();
            BeanUtils.copyProperties(buddy,vo);
            vo.setId(buddy.getId().toString());
            // 发起人
            vo.setInitiator(userMapper.getUserInfoById(buddy.getInitiatorId()));
            // 接受人
            vo.setReceiver(userMapper.getUserInfoById(buddy.getReceiverId()));

            return vo;
        }).toList();
        // 添加标识 区分是否是
        return buddyVos;
    }

    @Override
    public List<BuddyInfoVo> agreeBuddyList() {
        // 获取当前用户
        Long userId = securityContext.getUserId();
        // 获取当前用户相关的好友
        LambdaQueryWrapper<Buddy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Buddy::getStatus, IsBuddy.IS_BUDDY.getCode());
        wrapper.and(w->{
            w.eq(Buddy::getInitiatorId,userId);
            w.or();
            w.eq(Buddy::getReceiverId,userId);
        });
        List<Buddy> list = baseMapper.selectList(wrapper);
        List<BuddyInfoVo> BuddyInfoVo = list.stream().map(buddy -> {
            BuddyInfoVo vo = new BuddyInfoVo();
            // 获取用户
            this.getUserInfo(buddy, userId, vo);
            return vo;
        }).toList();
        return BuddyInfoVo;
    }

    private void getUserInfo(Buddy buddy, Long userId, BuddyInfoVo vo) {
        if(buddy.getInitiatorId().equals(userId)){ // 接受人
            vo.setUser(userMapper.getUserInfoById(buddy.getReceiverId()));
        }else { // 发起人
            vo.setUser(userMapper.getUserInfoById(buddy.getInitiatorId()));
        }
        BeanUtils.copyProperties(buddy,vo);
        vo.setId(buddy.getId().toString());

    }

    @Override
    public BuddyInfoVo getBuddyInfoById(Long id) {
        Buddy buddy = baseMapper.selectById(id);
        if(Objects.isNull(buddy) || !buddy.getStatus().equals(IsBuddy.IS_BUDDY.getCode())){//说明不是好友
            throw new RuntimeException("当前用户还不是您的好友");
        }
        Long userId = securityContext.getUserId();
        if(!buddy.getInitiatorId().equals(userId)&&!buddy.getReceiverId().equals(userId)){
            throw new RuntimeException("当前用户还不是您的好友");
        }
        BuddyInfoVo vo = new BuddyInfoVo();
        // 获取用户
        this.getUserInfo(buddy, securityContext.getUserId(), vo);

        return vo;
    }

    @Override
    public List<UserInfoVo> getShowBuddyList() {
        // 获取好友列表
        List<UserInfoVo>  vos =  buddyMapper.getShowBuddyList(securityContext.getUserId());
        return vos;
    }

    @Override
    public Boolean addShowBuddy(Long id) {
        return buddyMapper.addShowBuddy(securityContext.getUserId(),id);
    }
}
